#-----------------------------------------------------------------------------
#   Copyright (c) 2008-2009, David P. D. Moss. All rights reserved.
#
#   Released under the BSD license. See the LICENSE file for details.
#-----------------------------------------------------------------------------
"""Common code shared between various netaddr sub modules"""

import sys as _sys
import struct as _struct
import pprint as _pprint

#-----------------------------------------------------------------------------
#   Custom exceptions.
#-----------------------------------------------------------------------------
class AddrFormatError(Exception):
    """
    An Exception indicating a network address is not correctly formatted.
    """
    pass

#-----------------------------------------------------------------------------
class AddrConversionError(Exception):
    """
    An Exception indicating a failure to convert between address types or
    notations.
    """
    pass

#-----------------------------------------------------------------------------
def num_bits(int_val):
    """
    @param int_val: an unsigned integer.

    @return: the minimum number of bits needed to represent value provided.
    """
    int_val = abs(int_val)
    numbits = 0
    while int_val:
        numbits += 1
        int_val >>= 1
    return numbits
